/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.MrpMrp;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import merlion.common.util.Consts;
import merlion.mrp.ppm.entity.MRP;
import merlion.mrp.ppm.session.MRPSessionLocal;

/**
 *
 * @author Zhang Ying
 */
@ManagedBean
@ViewScoped
public class MrpMrpViewManagerBean {

    public static final String EDIT_MRP = "EditedMRP";
    public static final String VIEWED_MRP = "ViewedMrp";
    @EJB
    private MRPSessionLocal mrpSession;
    // for select page
    private Collection<MRP> allMRPs;
    private MRP viewedMRP;
    private String viewedMRPStrId;
    private ArrayList<Integer> counterForProduct;
    private ArrayList<Integer> counterForWeek;
    // for detail page viewing
    private MRP selectedMRP;
    private ArrayList<ArrayList<Long>> weeklyDemands;
    private Integer numOfWks;
    // for select edit
    private MRP editMRP;

    /** Creates a new instance of MrpMrpViewManagerBean */
    public MrpMrpViewManagerBean() {
        counterForProduct = new ArrayList<Integer>();
        for (int i = 0; i < 4; i++) {
            counterForProduct.add(i);
        }
        counterForWeek = new ArrayList<Integer>();
        for (int i = 0; i < 5; i++) {
            counterForWeek.add(i);
        }
    }

    // for select page
    public String redirectToViewMRPDetail() {
        if (viewedMRPStrId == null) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful", "Please select mrp."));
            return null;
        }
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(VIEWED_MRP);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(VIEWED_MRP, viewedMRP);
        return getRedirectPath(Consts.VIEW_MRP_MRP_PATH + "Detail");
    }

    public Collection<MRP> getAllMRPs() {
        if (allMRPs == null) {
            allMRPs = mrpSession.getAllMRPs();
        }
        return allMRPs;
    }

    public MRP getViewedMRP() {
        return viewedMRP;
    }

    public void setViewedMRP(MRP viewedMRP) {
        this.viewedMRP = viewedMRP;
        this.setViewedMRPStrId(viewedMRP.getMrpIdStr());
    }

    public String getViewedMRPStrId() {
        return viewedMRPStrId;
    }

    public void setViewedMRPStrId(String viewedMRPStrId) {
        this.viewedMRPStrId = viewedMRPStrId;
    }

    // for detail page
    public Long returnPrevInventory() {
        return mrpSession.getPlannedInventoryForPrevWeek(selectedMRP.getStartPeriodYear(), selectedMRP.getStartPeriodMonth(), selectedMRP.getBom());
    }

    public Double returnKG(Long l, int productId) {
        if (l == null) {
            return null;
        }
        switch (productId) {
            case 0:
                return 12.0 * l * selectedMRP.getBom().getMaterialRequirements().get(productId).getValue();
            case 1:
                return 12.0 * l * selectedMRP.getBom().getMaterialRequirements().get(productId).getValue();
            case 2:
                return 13.32 * l * selectedMRP.getBom().getMaterialRequirements().get(productId).getValue();
            case 3:
                return 13.32 * l * selectedMRP.getBom().getMaterialRequirements().get(productId).getValue();
            default:
                return 0.0;
        }
    }

    public Long returnGrossRequirements(int dayid) {
        if (dayid + 1 > getNumOfWks()) {
            return null;
        }
        return selectedMRP.getGrossRequirements().get(dayid).getLongValue();
    }

    public MRP getSelectedMRP() {
        if (selectedMRP == null) {
            MRP tmrp = (MRP) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(VIEWED_MRP);
            if (tmrp != null) {
                selectedMRP = tmrp;
                weeklyDemands = mrpSession.getWeeklyDemands(selectedMRP.getStartPeriodYear(), selectedMRP.getStartPeriodMonth());
                if (weeklyDemands.get(0).get(4) == null) {
                    numOfWks = 4;
                } else {
                    numOfWks = 5;
                }
            }
        }
        return selectedMRP;
    }

    public Integer getNumOfWks() {
        return numOfWks;
    }

    public void setSelectedMRP(MRP selectedMRP) {
        this.selectedMRP = selectedMRP;
    }

    public ArrayList<ArrayList<Long>> getWeeklyDemands() {
        if (weeklyDemands == null) {
            weeklyDemands = mrpSession.getWeeklyDemands(selectedMRP.getStartPeriodYear(), selectedMRP.getStartPeriodMonth());
        }
        return weeklyDemands;
    }

    public void setWeeklyDemands(ArrayList<ArrayList<Long>> weeklyDemands) {
        this.weeklyDemands = weeklyDemands;
    }

    // for rendering cell when < 5
    public boolean renderCell(Integer dayId) {
        if (dayId + 1 <= numOfWks) {
            return true;
        } else {
            return false;
        }
    }

    public boolean renderEmpty() {
        if (getNumOfWks() == 4) {
            return true;
        } else {
            return false;
        }
    }
    
    // for mrp edit page
    public String redirectToEditMRPDetail() {
        if (viewedMRPStrId == null) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful", "Please select mrp."));
            return null;
        }
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(EDIT_MRP);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(EDIT_MRP, editMRP);
        return getRedirectPath(Consts.EDIT_MRP_MRP_PATH + "Detail");
    }

    public MRP getEditMRP() {
        return editMRP;
    }

    public void setEditMRP(MRP editMRP) {
        this.editMRP = editMRP;
        this.setViewedMRPStrId(editMRP.getMrpIdStr());
    }

    // for general page
    public ArrayList<Integer> getCounterForProduct() {
        return counterForProduct;
    }

    public ArrayList<Integer> getCounterForWeek() {
        return counterForWeek;
    }

    private String getRedirectPath(String path) {
        return path + "?faces-redirect=true";
    }

    public String getMonthEnglish(int i) {
        switch (i) {
            case 1:
                return Consts.JAN;
            case 2:
                return Consts.FEB;
            case 3:
                return Consts.MAR;
            case 4:
                return Consts.APR;
            case 5:
                return Consts.MAY;
            case 6:
                return Consts.JUN;
            case 7:
                return Consts.JUL;
            case 8:
                return Consts.AUG;
            case 9:
                return Consts.SEP;
            case 10:
                return Consts.OCT;
            case 11:
                return Consts.NOV;
            case 12:
                return Consts.DEC;
            default:
                return "";
        }
    }

    private int getMonthIndex(String label) {
        if (label.equals(Consts.JAN)) {
            return 1;
        } else if (label.equals(Consts.FEB)) {
            return 2;
        } else if (label.equals(Consts.MAR)) {
            return 3;
        } else if (label.equals(Consts.APR)) {
            return 4;
        } else if (label.equals(Consts.MAY)) {
            return 5;
        } else if (label.equals(Consts.JUN)) {
            return 6;
        } else if (label.equals(Consts.JUL)) {
            return 7;
        } else if (label.equals(Consts.AUG)) {
            return 8;
        } else if (label.equals(Consts.SEP)) {
            return 9;
        } else if (label.equals(Consts.OCT)) {
            return 10;
        } else if (label.equals(Consts.NOV)) {
            return 11;
        } else if (label.equals(Consts.DEC)) {
            return 12;
        } else {
            return 0;
        }
    }

    public String returnProductAlpha(Integer i) {
        if (i.intValue() == 0) {
            //return Consts.ProductType.BAR_A.getText();
            return "A";
        } else if (i.intValue() == 1) {
            //return Consts.ProductType.BAR_B.getText();
            return "B";
        } else if (i.intValue() == 2) {
            //return Consts.ProductType.BAR_C.getText();
            return "C";
        } else if (i.intValue() == 3) {
            //return Consts.ProductType.BAR_D.getText();
            return "D";
        } else {
            return "";
        }
    }

    public String returnProductName(Integer i) {
        if (i.intValue() == 0) {
            return Consts.ProductType.BAR_A.getText();
        } else if (i.intValue() == 1) {
            return Consts.ProductType.BAR_B.getText();
        } else if (i.intValue() == 2) {
            return Consts.ProductType.BAR_C.getText();
        } else if (i.intValue() == 3) {
            return Consts.ProductType.BAR_D.getText();
        } else {
            return "";
        }
    }

    public String returnFormattedDay(Timestamp t) {
        if (t == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM");
        return sdf.format(t);
    }
}
